#include "Utils.h"
#include <time.h>
using namespace std;

const s32 BASICINFO_BATCH_LEN = 30;

string GetStrTime(u64 t)
{
    char str[64] = {0};
    time_t _t = (time_t)t;
    struct tm tmnow = { 0 };
    localtime_r( &_t, &tmnow );
    strftime( str, sizeof(str), "%Y-%m-%d %X", &tmnow );
    return std::string(str);
}

std::string GetNowTime()
{
    u64 _t = (u64)time(NULL);
    return GetStrTime(_t);
}

s32 SecPassFromDay(u64 t)
{
    if (0 == t) {
        t = (u64)time(NULL);
    }
    time_t _t = (time_t)t;
    struct tm tmnow = { 0 };
    localtime_r( &_t, &tmnow );
    return tmnow.tm_hour * 3600 + tmnow.tm_min*60 + tmnow.tm_sec;
}

s32 GetCurHour(u64 t)
{
    if (0 == t) {
        t = (u64)time(NULL);
    }
    time_t _t = (time_t)t;
    struct tm tmnow = { 0 };
    localtime_r( &_t, &tmnow );
    return tmnow.tm_hour; 
}

time_t Convert2Time(const char *strtime)
{
    if( !strtime )
    {   
        return 0;
    }   

    struct tm tm1;  
    time_t time1;  

    sscanf( strtime, "%4d-%2d-%2d %2d:%2d:%2d",    
            &tm1.tm_year,   
            &tm1.tm_mon,   
            &tm1.tm_mday,   
            &tm1.tm_hour,   
            &tm1.tm_min,  
            &tm1.tm_sec );  

    tm1.tm_year -= 1900;  
    tm1.tm_mon -= 1;  

    tm1.tm_isdst = -1;  

    time1 = mktime( &tm1 );  
    return time1;  
}

std::string GetCurMonth()
{
    char strtime[100];  
    time_t utc_time = time(NULL);  
    struct tm local_time;
    localtime_r( &utc_time, &local_time );  
    strftime( strtime, sizeof(strtime), "%Y%m", &local_time );
    return string(strtime);
}

bool IsSameDay(u64 t1, u64 t2)
{
    // one hour = 3600 sec
    // one day = 86400 sec
    if( (t1 / 86400) == (t2 / 86400) )    
    {            
        return true;    
    }    
    return false;
}

s32 GetCurDay()
{
    char strtime[100];  
    time_t utc_time = time(NULL);  
    struct tm local_time;
    localtime_r( &utc_time, &local_time );  
    strftime( strtime, sizeof(strtime), "%Y%m%d", &local_time );
    return atoi(strtime);
}

std::string ConvertIp(u32 ip)
{
    const int IP_LEN = 32;
    char strip[IP_LEN] = {0};
    ip = ntohl(ip);
    snprintf( strip, IP_LEN, "%d.%d.%d.%d",
        ip >> 24,
        (ip >> 16) & 0x000000FF,
        (ip >> 8) & 0x000000FF,
        ip & 0x000000FF );
    return string(strip);
}

static unsigned char ToHex(unsigned char x)
{
    return  x > 9 ? x + 55 : x + 48;
}

std::string UrlEncode(const std::string& str)
{
    std::string strTemp = "";
    size_t length = str.length();
    for (size_t i = 0; i < length; i++)
    {
        if (isalnum((unsigned char)str[i]) ||
            (str[i] == '-') ||
            (str[i] == '_') ||
            (str[i] == '.') ||
            (str[i] == '~'))
            strTemp += str[i];
        else if (str[i] == ' ')
            strTemp += "+";
        else
        {
            strTemp += '%';
            strTemp += ToHex((unsigned char)str[i] >> 4);
            strTemp += ToHex((unsigned char)str[i] % 16);
        }
    }
    return strTemp;
}

void SplitString(const char *str, char ch, std::vector<std::string> &vec)
{
    const char *ptr = str;
    while(ptr)
    {
        const char *pos = strchr(ptr, ch);
        if( pos == NULL )
        {
            vec.push_back(ptr);
            break;
        }

        vec.push_back( std::string(ptr, pos) );
        ptr = pos + 1;
    }
}


